package club.banyuan.hrms.dao.impl;

import club.banyuan.hrms.dao.AdminDao;
import club.banyuan.hrms.domain.Admin;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class AdminDaoImpl implements AdminDao {
    private static List<Admin> admins;

    static {

        admins = new ArrayList<>();

        Admin admin = new Admin();
        admin.setId(1);
        admin.setUsername("admin");
        admin.setRolename("超级管理员");
        admin.setPassword("123456");

        admins.add(admin);

        // 创建部门管理员
        admin = new Admin();
        admin.setId(2);
        admin.setUsername("dept");
        admin.setRolename("部门管理员");
        admin.setPassword("1234");

        admins.add(admin);
    }

    @Override
    public List<Admin> getAdmins() {
        return admins;
    }

    @Override
    public Admin getAdminByUsernameAndPassword(String username, String password) {
        List<Admin> admins = getAdmins();

        for (Admin admin : admins) {
            if ((admin.getUsername() != null) && (admin.getUsername().equals(username)) && (admin.getPassword() != null) && (admin.getPassword().equals(password))) {
                return admin;
            }
        }
        return null;
    }

    @Override
    public void addAdmin(Admin admin) {
        admin.setId(admins.size() + 1);
        admins.add(admin);
    }

    @Override
    public void updateAdmin(Admin admin) {
        Integer id = admin.getId();
        if (id != null) {
            Admin existAdmin = getAdminById(id);
            existAdmin.setUsername(admin.getUsername());
            existAdmin.setRolename(admin.getRolename());
            existAdmin.setPassword(admin.getPassword());
        }
    }

    @Override
    public Admin getAdminById(Integer id) {
        for (Admin admin : admins) {
            if (admin.getId().equals(id)) {
                return admin;
            }
        }
        return null;
    }

    @Override
    public void deleteAdmin(Integer[] ids) {

    }
}
